package com.masterclient.app.modules.purchase.adapter;

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.RelativeSizeSpan;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.load.Transformation;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.eoner.baselibrary.bean.purchase.PurchaseProductBean;
import com.eoner.baselibrary.utils.ScreenUtils;
import com.eoner.baselibrary.utils.ViewTransformUtil;
import com.masterclient.app.R;
import com.masterclient.app.widget.CenterImageSpan;
import com.masterclient.app.widget.PriceIntegralLayout;
import com.masterclient.app.widget.TagTextView;

import butterknife.BindView;
import butterknife.ButterKnife;
import jp.wasabeef.glide.transformations.RoundedCornersTransformation;

/**
 * @auther hjian
 * @date 2020/9/17.
 */
public class PurchaseProductAdapter extends BaseQuickAdapter<PurchaseProductBean, PurchaseProductAdapter.ViewHolder> {

    private String gift_icon;
    public PurchaseProductAdapter() {
        super(R.layout.item_purchase);
        gift_icon = "";
    }

    public void setGift_icon(String gift_icon) {
        this.gift_icon = gift_icon;
    }

    @Override
    protected void convert(ViewHolder helper, PurchaseProductBean item) {


        ViewTransformUtil.glideImageView(mContext, item.getImage(), helper.itemPurchaseImg, new Transformation[]{new CenterCrop(),
                        new RoundedCornersTransformation(ScreenUtils.dp2px(7), 0)}, R.mipmap.defaults);
        ViewTransformUtil.glideImageView(mContext, gift_icon, helper.itemPurchaseLogo, R.color.transparent);

//        helper.itemPurchaseName.setText(item.getTitle());
//        List<String> tagList = new ArrayList<>();
//        tagList.add(item.getGift_text());
//        helper.itemPurchaseName.setContentAndTag((tagList.size()>0?" ":"")  + item.getTitle(),tagList,false,true);
//        helper.itemPurchaseName.setMaxLines(1);
        if (!TextUtils.isEmpty(item.getGift_text())){
            setTextTag(helper.itemPurchaseName, item.getGift_text(),item.getTitle());
        }else {
            helper.itemPurchaseName.setText(item.getTitle());
        }
        String againBuy = item.getGrand_text();
        String purchasedText = item.getPurchased_text();
        if (!TextUtils.isEmpty(againBuy)) {
            helper.viewPurchaseAgainBuy.setVisibility(View.VISIBLE);
            helper.itemPurchaseHeadAgainBuy.setText(againBuy);
            if (!TextUtils.isEmpty(item.getNext_price_desc())){
                helper.viewPurchaseLine.setVisibility(View.VISIBLE);
                helper.itemPurchaseAgainBuy.setText(String.format(" %s", item.getNext_price_desc()));
                helper.itemPurchaseHeadAgainBuy.setText(String.format("%s ", againBuy));
            }else {
                helper.viewPurchaseLine.setVisibility(View.GONE);
            }
        }else {
            helper.viewPurchaseAgainBuy.setVisibility(View.GONE);
        }
        if (!TextUtils.isEmpty(purchasedText)) {
            helper.itemPurchaseTotalNum.setVisibility(View.VISIBLE);
            helper.itemPurchaseTotalNum.setText(purchasedText);
        }else {
            helper.itemPurchaseTotalNum.setVisibility(View.GONE);
        }

        helper.itemPurchaseLine.setVisibility(helper.getAdapterPosition() == getData().size()-1?View.GONE:View.VISIBLE);

//        Utils.setDin(helper.itemPurchasePrice,mContext);

        String priceStr = new StringBuffer("¥ ").append(item.getPrice()).toString();
        SpannableString spannableString = new SpannableString(priceStr);
        spannableString.setSpan(new RelativeSizeSpan(0.57f), 0, 2, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        if (item.getPrice().contains(".")){
            spannableString.setSpan(new RelativeSizeSpan(0.71f), priceStr.indexOf("."), priceStr.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        }
        helper.itemPurchasePrice.setData("0", item.getPrice());


        helper.addOnClickListener(R.id.item_purchase_select);
    }

    private void setTextTag(TextView tv, String strTag, String content){
        StringBuffer buffer = new StringBuffer(strTag);
        buffer.append(content);
        SpannableString spannableString = new SpannableString(buffer.toString());
        TextView tvTag = new TextView(mContext);
        tvTag.setText(strTag);
        tvTag.setTextSize(10);
        tvTag.setTextColor(mContext.getResources().getColor(R.color.white));
        tvTag.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ScreenUtils.dp2px(14)));
        tvTag.setGravity(Gravity.CENTER);
        tvTag.setIncludeFontPadding(false);
        tvTag.setPadding(ScreenUtils.dp2px(4),ScreenUtils.dp2px(1),ScreenUtils.dp2px(4),ScreenUtils.dp2px(1));
        tvTag.setPadding(ScreenUtils.dp2px(2),ScreenUtils.dp2px(1),ScreenUtils.dp2px(2),ScreenUtils.dp2px(1));
        tvTag.setBackgroundResource(R.drawable.bg_purchase_tag);
        tvTag.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
        tvTag.layout(0, 0, tvTag.getMeasuredWidth(), tvTag.getMeasuredHeight());
        tvTag.buildDrawingCache();
        Bitmap bitmap = tvTag.getDrawingCache();
        Drawable d = new BitmapDrawable(bitmap);
        d.setBounds(0, 0, tvTag.getWidth(), tvTag.getHeight());//缺少这句的话，不会报错，但是图片不回显示
        CenterImageSpan span = new CenterImageSpan(d,0, ScreenUtils.dp2px(4));
        spannableString.setSpan(span, 0, strTag.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        tv.setText(spannableString);
        tv.setGravity(Gravity.CENTER_VERTICAL);
    }


    class ViewHolder extends BaseViewHolder {
        @BindView(R.id.item_purchase_img)
        ImageView itemPurchaseImg;
        @BindView(R.id.item_purchase_logo)
        ImageView itemPurchaseLogo;
        @BindView(R.id.item_purchase_name)
        TagTextView itemPurchaseName;
        @BindView(R.id.ll_purchase_again_buy)
        View viewPurchaseAgainBuy;
        @BindView(R.id.item_purchase_head_again_buy)
        TextView itemPurchaseHeadAgainBuy;
        @BindView(R.id.view_purchase_line)
        View viewPurchaseLine;
        @BindView(R.id.item_purchase_again_buy)
        TextView itemPurchaseAgainBuy;
        @BindView(R.id.item_purchase_total_num)
        TextView itemPurchaseTotalNum;
        @BindView(R.id.item_purchase_select)
        View itemPurchaseSelect;
        @BindView(R.id.item_purchase_price_desc)
        TextView itemPurchasePriceDesc;
        @BindView(R.id.item_purchase_price)
        PriceIntegralLayout itemPurchasePrice;
        @BindView(R.id.item_purchase_line)
        View itemPurchaseLine;

        public ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
